<template>
    <div>
<h1>新增康复信息</h1>
<table class="table">
    <tbody>
        <tr>
            <td>code</td>
            <td>
                <input type="text" v-model="data.code" disabled >
            </td>
        </tr>
        <tr>
            <td>name</td>
            <td>
                <input type="text" v-model="data.name">
            </td>
        </tr>
        <tr>
            <td>isDeleted</td>
            <td>
                <input v-model="data.isDeleted" type="radio" name="isDeleted" :value="true" > 已删除
                <input v-model="data.isDeleted" type="radio" name="isDeleted" :value="false" checked > 未删除
            </td>
        </tr>
        <tr>
            <td>start</td>
            <td>
                <input v-model="data.start" type="radio" name="start" :value="true" > 启用
                <input v-model="data.start" type="radio" name="start" :value="false" checked > 禁用
            </td>
        </tr>
        <tr>
            <td>sericesTime</td>
            <td>
                <input type="text" v-model="data.sericesTime" > 
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <input type="button" value="新增" @click="saveinfo">
            </td>
        </tr>
    </tbody>
</table>
    </div>
</template>

<script setup lang="ts">

import { ref,onMounted } from 'vue';
import moment  from 'moment';
import axios from 'axios';
import { useRouter } from 'vue-router';

const router = useRouter()
const data=ref({
  "id": 0,
  "code": "",
  "name": "",
  "isDeleted": true,
  "start": true,
  "sericesTime": 0
})


onMounted(()=>{
    setcode()
})
//添加时康复理疗项目编号自动生成，格式为yyyyMMdd+3位流水号
const setcode=()=>{
    //取随机数
    //取日期字符串
    var n = Math.random().toString().substring(2,5);
    var d =  moment().format('YYYYMMDD');

    data.value.code=d+n;
}

//保存
const saveinfo=()=>{
    //服务人数和服务时长必须是整数判定 
    if(data.value.name==""){
        alert("康复名称不能为空")
        return;
    }

    //服务时长必须是整数判定 
    if(data.value.sericesTime*0 != 0){
        alert("服务时长必须是整数判定 ")
        return;   
    } 
    //找有没有包含数值“点”
    if(data.value.sericesTime.toString().lastIndexOf('.') != -1  ){
        alert("服务时长必须是整数判定 ")
        return;   
    } 


    axios.post("https://localhost:7043/api/Rehabilitation/AddRehabilitations",data.value).then(res=>{
        if(res.data>0){
            alert('新增成功')
            router.push({path:'/'})
        }
        else{
            alert('新增失败了。。')
        }
    })
}
</script>

<style scoped>

</style>